package com.yunhui.wx.robot.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yunhui.wx.robot.bean.request.BaseReqMessage;
import com.yunhui.wx.robot.bean.response.BaseRespMessage;
import com.yunhui.wx.robot.bean.response.RobotResponTypeEnum;
import com.yunhui.wx.robot.common.BaseCommon;
import com.yunhui.wx.robot.util.HttpRequestUtil;
import com.yunhui.wx.robot.util.TLRobotUtil;

/**
 * @Author: Yun
 * @Description: 图灵机器人的服务类
 * @Date: Created in 2017-11-30 10:51
 */
public class TlRobotService {

    /**
     * 调用图灵的问答API,并返回文本响应值
     * @param userid
     * @param info
     * @return
     */
    public static String getTextResponse(String userid,String info){
        //封装请求参数对象
        BaseReqMessage reqMessage=new BaseReqMessage();
        reqMessage.setKey(BaseCommon.TL_ROBOT_API_KEY);
        reqMessage.setInfo(info);
        reqMessage.setUserid(userid);
        //转换为json
        ObjectMapper mapper=new ObjectMapper();
        try {
            String json=mapper.writeValueAsString(reqMessage);
            String result=HttpRequestUtil.doPost(BaseCommon.TL_ROBOT_API_URL,json,"UTF-8");
            BaseRespMessage respMessage=mapper.readValue(result,BaseRespMessage.class);
            RobotResponTypeEnum robotResponTypeEnum=RobotResponTypeEnum.getRobotResponseType(respMessage.getCode(),respMessage.getText());
            return robotResponTypeEnum.getText();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 调用图灵的问答API2.0,并返回文本响应值
     * @param userid
     * @param info
     * @return
     */
    public static String getTextResponseV2(String userid,String info){
        //获取封装请求参数字符串
    	String req = TLRobotUtil.getReqMes(info, userid);
    	//调用图灵机器人API2.0接口
    	String res = HttpRequestUtil.doPost(BaseCommon.TL_ROBOT_API_URL_V2, req, "UTF-8");
    	return TLRobotUtil.getResultMes(res);
    }
    
}
